package com.netted.operationassistant.activity;

import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.mylhyl.superdialog.SuperDialog;
import com.netted.operationassistant.R;
import com.netted.operationassistant.constant.Constant;
import com.netted.operationassistant.entity.Port;
import com.netted.operationassistant.http.HttpRequest;

import java.util.HashMap;
import java.util.Map;

//public class DetectionFinish2Activity extends BaseActivity  implements QRCodeReaderView.OnQRCodeReadListener {
public class DetectionFinish2Activity extends BaseActivity   {
    /*private String params = "";
    private JSONObject paramsJson = null;
    private TextView resultTextView;
    private QRCodeReaderView qrCodeReaderView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detection_finish2);
        qrCodeReaderView = (QRCodeReaderView) findViewById(R.id.qrdecoderview);
        qrCodeReaderView.setOnQRCodeReadListener(this);

        resultTextView = (TextView)findViewById(R.id.ddd123);

        // Use this function to enable/disable decoding
//        qrCodeReaderView.setQRDecodingEnabled(true);

        // Use this function to change the autofocus interval (default is 5 secs)
        qrCodeReaderView.setAutofocusInterval(2000L);

        // Use this function to enable/disable Torch
//        qrCodeReaderView.setTorchEnabled(true);

        // Use this function to set front camera preview
        qrCodeReaderView.setFrontCamera();

        // Use this function to set back camera preview
//        qrCodeReaderView.setBackCamera();


        params = getIntent().getStringExtra("params");
        if (params == null || "".equals(params)){
            showDialog("出错啦", "非正常进入", new SuperDialog.OnClickPositiveListener() {
                @Override
                public void onClick(View v) {
                    DetectionFinish2Activity.this.finish();
                }
            });
        }else{
            paramsJson = JSONObject.parseObject(params);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        qrCodeReaderView.startCamera();
    }

    @Override
    public void onQRCodeRead(String text, PointF[] points) {
        resultTextView.setText(text);
        ToastUtils.showLong(text);
    }

    public void exitActivity(View view){
        this.finish();
    }



    public void checkDoorDialog(final String port){
        showDialog("提示", "仓门已打开,请放入平板后关好仓门", new SuperDialog.OnClickPositiveListener() {
            @Override
            public void onClick(View v) {
                if (!checkDoor(port)){
                    checkDoorDialog(port);
                }
            }
        });
    }

    public void doData(String res){
        if (res != null){
            JSONObject json = JSONObject.parseObject(res);


            LogUtils.e("params############:"+params);
            LogUtils.e("result############:"+res);
            String timeTag = paramsJson.getString("time");
            String time = json.getString("time");

            String catbinetIdTag = paramsJson.getString("catbinetId");
            String catbinetId = json.getString("catbinetId");

            String tokenTag = paramsJson.getString("token");
            String token = json.getString("token");
            String portTag = paramsJson.getString("port");
            final String port = json.getString("port");


            if (portTag == null || !portTag.equals(port)){
                showDialog("提示", "无效平板", new SuperDialog.OnClickPositiveListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                return;
            }
            if (timeTag == null || !timeTag.equals(time)){
                showDialog("提示", "无效平板", new SuperDialog.OnClickPositiveListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                return;
            }

            if (catbinetIdTag == null || !catbinetIdTag.equals(catbinetId)){
                showDialog("提示", "无效平板", new SuperDialog.OnClickPositiveListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                return;
            }

            if (tokenTag == null || !tokenTag.equals(token)){
                showDialog("提示", "无效平板", new SuperDialog.OnClickPositiveListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                return;
            }

            if (Long.parseLong(time) - System.currentTimeMillis() > (1000*60*10)){
                showDialog("提示", "操作超时,请重新检测", new SuperDialog.OnClickPositiveListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                return;
            }

            String result = json.getString("result");
            if ("success".equals(result)){
                try {
                    openPort(port);
                    Thread.sleep(1000);
                    if ("".equals(serialValue)){
                        showDialog("提示", "仓门打开失败,请重新检测", new SuperDialog.OnClickPositiveListener() {
                            @Override
                            public void onClick(View v) {
                                finish();
                            }
                        });
                    }else{
                        checkDoorDialog(port);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else{
                showDialog("提示", "您的平板未能通过检测,请重新检测或通过家长小程序提交报修流程", new SuperDialog.OnClickPositiveListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                return;
            }

        }else{
            ToastUtils.showLong("二维码错误");
            finish();
        }
    }

    public boolean checkDoor(String port){
        try {
            getPortStatus(port);
            Thread.sleep(1000);
            if ("".equals(serialValue)){
                return false;
            }else{
                Port p = hexToPort(serialValue);
                //如果设备不在仓内或者仓门没关闭,则返回false
                if (!"1".equals(p.getIsHas()) || !"1".equals(p.getIsOpen())){
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
            return false;
        }
        return true;
    }

    public void finish(){

        startActivity(new Intent(this,MenuActivity.class));
        DetectionFinish2Activity.this.finish();

    }

    *//**
     * 打开仓门
     *//*
    public void openDoor(String catbinetId,String token, String params){
        Map<String,String> paramMap = new HashMap<>();
        paramMap.put("catbinetId",catbinetId);
        paramMap.put("token",token);
        paramMap.put("params",params);
        String url = Constant.HOST+"/ws/opt/put";
        request.post(url, paramMap, "post", new HttpRequest.Callback() {
            @Override
            public void onSuccess(String response) {
                if (!StringUtils.isEmpty(response)){
                    JSONObject resJson = JSONObject.parseObject(response);
                    if (!"success".equals(resJson.getString("flag"))){
                        showDialog("提示",resJson.getString("data")+"仓门已打开，请归还平板后关闭仓门",null);
                    }
                }

            }

            @Override
            public void onError(VolleyError error) {

            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
//        qrCodeReaderView.flu
    }*/
}
